package com.cn.client;

import com.cn.common.MessageQueue;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class MessageQueueLock {

    private ConcurrentMap<MessageQueue, Object> lockMap = new ConcurrentHashMap<>();

    public Object lock(MessageQueue messageQueue) {
        Object old = this.lockMap.get(messageQueue);
        if (null == old) {
            old = new Object();
            Object pre = this.lockMap.putIfAbsent(messageQueue, old);
            if (pre != null) {
                old = pre;
            }
        }
        return old;
    }

}
